package com.wanjia.estate.controller;

import cn.com.infosec.netsign.json.JsonObject;
import cn.hutool.json.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.spire.pdf.FileFormat;
import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;
import com.spire.pdf.graphics.PdfImage;
import com.wanjia.estate.exception.PaperDoorException;
import com.wanjia.estate.exception.TaoCiDataException;
import com.wanjia.estate.pojo.entity.*;
import com.wanjia.estate.pojo.model.JsonResult;
import com.wanjia.estate.pojo.print.PaperDrive;
import com.wanjia.estate.pojo.print.Stamp;
import com.wanjia.estate.pojo.tax.PdfFile;
import com.wanjia.estate.properties.*;
import com.wanjia.estate.service.*;

import com.wanjia.estate.service.bdservice.bdcdjxxcxjgzmservice.*;
import com.wanjia.estate.service.bdservice.FjtService.ExcelAddImgFjtService;
import com.wanjia.estate.service.bdservice.FjtService.ExcelReaderWriterFjtService;
import com.wanjia.estate.service.httpservice.NewPrintHttpService;
import com.wanjia.estate.service.httpservice.PrintHttpService;
import com.wanjia.estate.service.bdservice.sjdspireservice.ExcelAddImgSjdService;
import com.wanjia.estate.service.bdservice.sjdspireservice.ExcelReaderWriterSjdService;
import com.wanjia.estate.service.bdservice.ydzfkplxdsprieservice.ExcelAddQrService;
import com.wanjia.estate.service.bdservice.ydzfkplxdsprieservice.ExcelReaderWriterYdzfkplxdService;
import com.wanjia.estate.service.bdservice.zjsbdcjysbdservice.ExcelAddImgZjsbdcjysbdService;
import com.wanjia.estate.service.bdservice.zjsbdcjysbdservice.ExcelReaderWriterZjsbdcjysbdService;
import com.wanjia.estate.service.bdservice.zlfjyspireservice.ExcelAddImgZlfService;
import com.wanjia.estate.service.bdservice.zlfjyspireservice.ExcelReaderWriterZlfService;
import com.wanjia.estate.service.bdservice.zydjspireservice.ExcelAddImgService;
import com.wanjia.estate.service.bdservice.zydjspireservice.ExcelReaderWriterZydjService;
import com.wanjia.estate.utils.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/conversion")
@Slf4j
@Tag(name = "不动产表单接口控制器")
public class BdcBdController extends BaseController{

    @Autowired
    ExcelReaderWriterBdcService  excelReaderWriterBdcService;

    @Autowired
    private FolderPathProperties folderPathProperties;

    @Autowired
    ExcelAddImgBdcService excelAddImgBdcService;

    @Autowired
    LinuxPrintProperties linuxPrintProperties;


    @Setter(onMethod_ = @Autowired)
    private NewPrintHttpService newService;

    @Autowired
    PdfAddImageService pdfAddImageService;

    @Autowired
    LinuxPdfProperties linuxPdfProperties;

    @Autowired
    BdcDataService bdcDataService;

    @Autowired
    ExcelReaderWriterZydjService excelReaderWriterZydjService;

    @Autowired
    ExcelAddImgService excelAddimgService;

    @Autowired
    ExcelReaderWriterSjdService excelReaderWriterSjdService;

    @Autowired
    ExcelAddImgSjdService excelAddImgSjdService;

    @Autowired
    ExcelReaderWriterYdzfkplxdService excelReaderWriterYdzfkplxdService;

    @Autowired
    ExcelAddQrService excelAddQrService;

    @Autowired
    ExcelAddImgZjsbdcjysbdService excelAddImgZjsbdcjysbdService;

    @Autowired
    ExcelReaderWriterZjsbdcjysbdService excelReaderWriterZjsbdcjysbdService;

    @Autowired
    ExcelReaderWriterZlfService excelReaderWriterZlfService;

    @Autowired
    ExcelAddImgZlfService excelAddImgZlfService;

    @Autowired
    ExcelAddImgFjtService excelAddImgFjtService;

    @Autowired
    ExcelReaderWriterFjtService excelReaderWriterFjtService;

    @Autowired
    LinuxDocxProperties linuxDocxProperties;

    @Autowired
    private FileTemplateProperties fileTemplateProperties;

    @Value("${sealpath}")
    private String sealpath;

    @Value("${bdccwsealpath}")
    private String bdccwsealpath;

    @Autowired
    SearchHomeService searchHomeService;

    @Autowired
    ExcelReaderWriterBdc2Service excelReaderWriterBdc2Service;



    @Operation(summary = "不动产登记信息查询结果证明接口(唯一或第二套)")
    @PostMapping("/printBdc2")
    public JsonResult<JSONObject> writeZlfExcel2(@RequestBody TaoCi taoCi) throws Exception {
        //唯一或第二套住房，获取所有身份证信息查询房屋套次，然后去重
        List<Details> details = taoCi.getDetails();
        Set<String> set = new HashSet<>();
        List<Bdcdjxxjgcx> bdcdjxxjgcxList = new ArrayList<>();
        for (Details detail : details) {
            List<Bdcdjxxjgcx> bdcdjxxjgcxes = searchHomeService.SearchHome(detail);
            bdcdjxxjgcxes.stream()
                    .filter(b -> !set.contains(b.getCqzh()))
                    .forEach(b -> {
                        set.add(b.getCqzh());
                        bdcdjxxjgcxList.add(b);
                    });
        }
        int count=0;
        for (Bdcdjxxjgcx bdcdjxxjgcx : bdcdjxxjgcxList) {
            if (bdcdjxxjgcx.getFwyt().equals("住宅")){
                count++;
            }
        }
//        int size = bdcdjxxjgcxList.size();
        log.info("房产总数：{}",count);

        Bdcdjxxjgcx bdcdjxxjgcx= new Bdcdjxxjgcx();
        bdcdjxxjgcx.setYcslywh(taoCi.getYcslywh());
        bdcdjxxjgcx.setYwlx(taoCi.getYwlx());
        bdcdjxxjgcx.setCxrlx(taoCi.getCxrlx());

        bdcdjxxjgcx.setCxr1(taoCi.getCxr1());
        bdcdjxxjgcx.setCqzh1(taoCi.getCqzh1());
        bdcdjxxjgcx.setSqsj(taoCi.getSqsj());
        bdcdjxxjgcx.setFwyt1(taoCi.getFwyt1());
        bdcdjxxjgcx.setZl1(taoCi.getZl1());
        bdcdjxxjgcx.setGyfs1(taoCi.getGyfs1());

        File folder=new File(folderPathProperties.getFilepath());
        if (!folder.exists()) {
            folder.mkdir();
        }


        String ExcelPath = excelReaderWriterBdc2Service.ExcelReaderWriter(bdcdjxxjgcxList,taoCi,bdcdjxxjgcx,folderPathProperties.getFilepath());

        //获取文件地址和pdf文件存放地址
        File file = new File(ExcelPath);
        File file1 = new File(folderPathProperties.getPdfpath());
        File file2 = new File(folderPathProperties.getFilepath());

        String name = file.getName();

        String name1= file1.getParentFile().getName()+File.separator+file1.getName();
        String name2 = file2.getName();

        //调用第三方生成pdf格式文件

        String cmd = linuxPdfProperties.getPdfprefix()+" "+linuxPdfProperties.getPdfmap()+linuxPdfProperties.getPdfmiddle()+" "+linuxPdfProperties.getPdfxls()+name2+"/"+name+" "+linuxPdfProperties.getPdfpdf()+name1 +"/"+taoCi.getTcName()+".pdf";

        LinuxUtil.LinuxPdf(cmd);
        Thread.sleep(10000L);

        //电子章贴图
        pdfAddImageService.PdfAddImage(folderPathProperties.getPdfpath()+"/"+taoCi.getTcName()+".pdf",sealpath,100f,250f,0.18f,0.18f);
//        Thread.sleep(10000L);
//        PaperDrive();
//        Thread.sleep(10000L);
//        LinuxPrintUtil.linuxPrint(linuxPrintProperties.getPrintprefix(),folderPathProperties.getPdfpath()+"/tc.pdf",linuxPrintProperties.getPrintername(),linuxPrintProperties.getPrintsuffix1());
//        Thread.sleep(15000L);
//        PaperStop();
//        Thread.sleep(10000L);
//        newService.newRepeatOpenPaperDoor();
//        FolderUtil.deleteFolder(folder);
        JSONObject dataTc = new JSONObject();
        dataTc.set("size",count);
        dataTc.set("address",folderPathProperties.getNginxpath() + "/"+taoCi.getTcName()+".pdf");
        return new JsonResult<>(OK,dataTc);
    }


    @Operation(summary = "不动产登记信息查询结果证明接口(三套以上)")
    @PostMapping("/printBdc")
    public JsonResult<JSONObject> writeZlfExcel(@RequestBody TaoCi taoCi) throws Exception {
        List<Bdcdjxxjgcx> qlrlist = new ArrayList<>();
        List<Bdcdjxxjgcx> xcrlist = new ArrayList<>();
        List<Details> details = taoCi.getDetails();
        for (Details detail : details) {
            List<Bdcdjxxjgcx> list = bdcDataService.BdcDataIntegration(detail);
            for (Bdcdjxxjgcx bdcdjxxjgcx1 : list) {
                if (!bdcdjxxjgcx1.getCxr().isEmpty()){
                    qlrlist.add(bdcdjxxjgcx1);
                }else{
                    xcrlist.add(bdcdjxxjgcx1);
                }
            }
        }

        log.info("房产总数：{}",qlrlist.size()+xcrlist.size());
        //比对房产数量与承诺书三套以上是否一致，不一致直接报错，一致进行打印

        Bdcdjxxjgcx bdcdjxxjgcx= new Bdcdjxxjgcx();
        bdcdjxxjgcx.setYcslywh(taoCi.getYcslywh());
        bdcdjxxjgcx.setYwlx(taoCi.getYwlx());
        bdcdjxxjgcx.setCxrlx(taoCi.getCxrlx());

        bdcdjxxjgcx.setCxr1(taoCi.getCxr1());
        bdcdjxxjgcx.setCqzh1(taoCi.getCqzh1());
        bdcdjxxjgcx.setSqsj(taoCi.getSqsj());
        bdcdjxxjgcx.setFwyt1(taoCi.getFwyt1());
        bdcdjxxjgcx.setZl1(taoCi.getZl1());
        bdcdjxxjgcx.setGyfs1(taoCi.getGyfs1());


        File folder=new File(folderPathProperties.getFilepath());
        if (!folder.exists()) {
            folder.mkdir();
        }

        String ExcelPath = excelReaderWriterBdcService.ExcelReaderWriter(qlrlist,xcrlist,taoCi,bdcdjxxjgcx,folderPathProperties.getFilepath());


        //获取文件地址和pdf文件存放地址
        File file = new File(ExcelPath);
        File file1 = new File(folderPathProperties.getPdfpath());
        File file2 = new File(folderPathProperties.getFilepath());

        String name = file.getName();

        String name1= file1.getParentFile().getName()+File.separator+file1.getName();
        String name2 = file2.getName();

        //调用第三方生成pdf格式文件
        String cmd = linuxPdfProperties.getPdfprefix()+" "+linuxPdfProperties.getPdfmap()+linuxPdfProperties.getPdfmiddle()+" "+linuxPdfProperties.getPdfxls()+name2+"/"+name+" "+linuxPdfProperties.getPdfpdf()+name1 +"/"+taoCi.getTcName()+".pdf";
        LinuxUtil.LinuxPdf(cmd);
        Thread.sleep(10000L);

        //电子章贴图
         pdfAddImageService.PdfAddImage(folderPathProperties.getPdfpath()+"/"+taoCi.getTcName()+".pdf",sealpath,100f,250f,0.18f,0.18f);
//        Thread.sleep(10000L);
//        PaperDrive();
//        Thread.sleep(10000L);
//        LinuxPrintUtil.linuxPrint(linuxPrintProperties.getPrintprefix(),folderPathProperties.getPdfpath()+"/tc.pdf",linuxPrintProperties.getPrintername(),linuxPrintProperties.getPrintsuffix1());
//        Thread.sleep(15000L);
//        PaperStop();
//        Thread.sleep(10000L);
//        newService.newRepeatOpenPaperDoor();
//        FolderUtil.deleteFolder(folder);
        JSONObject dataTc = new JSONObject();
        dataTc.set("size",qlrlist.size()+xcrlist.size());
        dataTc.set("address",folderPathProperties.getNginxpath() + "/"+taoCi.getTcName()+".pdf");
        return new JsonResult<>(OK,dataTc);
    }



    @Operation(summary = "填写镇江市不动产转移登记申请书接口")
    @PostMapping("/printZydj")
    public JsonResult<Object> writeZydjExcel(@RequestBody Identity identity) throws Exception {

        File folder=new File(folderPathProperties.getFilepath());
        if (!folder.exists()) {
            folder.mkdir();
        }
        //传入数据生成新的excel
        String ExcelPath = excelReaderWriterZydjService.ExcelReaderWriter(identity,folderPathProperties.getFilepath());

        String newExcelPath = excelAddimgService.ExcelAddPng(ExcelPath, identity);

        File folder1=new File(folderPathProperties.getPdfpath());
        if (!folder1.exists()) {
            folder1.mkdir();
        }


        File file = new File(newExcelPath);
        File file1 = new File(folderPathProperties.getPdfpath());
        File file2 = new File(folderPathProperties.getFilepath());
        String name = file.getName();
        String name1= file1.getParentFile().getName()+File.separator+file1.getName();
        String name2 = file2.getName();

        String cmd = linuxPdfProperties.getPdfprefix()+" "+linuxPdfProperties.getPdfmap()+linuxPdfProperties.getPdfmiddle()+" "+linuxPdfProperties.getPdfxls()+name2+"/"+name+" "+linuxPdfProperties.getPdfpdf()+name1+"/a.pdf";
        LinuxUtil.LinuxPdf(cmd);
        Thread.sleep(10000L);

        PaperDrive();
        LinuxPrintUtil.linuxPrint(linuxPrintProperties.getPrintprefix(),folderPathProperties.getPdfpath()+"/a.pdf",linuxPrintProperties.getPrintername(),linuxPrintProperties.getPrintsuffix1());
        Thread.sleep(15000L);

        PaperStop();
        Thread.sleep(10000L);

        newService.newRepeatOpenPaperDoor();
        FolderUtil.deleteFolder(folder);
        return new JsonResult<>(OK);
    }


    @Operation(summary = "收件单接口")
    @PostMapping("/printSjd")
    public JsonResult<Object> writeZlfExcel(@RequestBody Sjd sjd) throws Exception {

        File folder=new File(folderPathProperties.getFilepath());
        if (!folder.exists()) {
            folder.mkdir();
        }
        String ExcelPath = excelReaderWriterSjdService.ExcelReaderWriter(sjd,folderPathProperties.getFilepath());
        String newExcelPath = excelAddImgSjdService.ExcelAddImg(ExcelPath, sjd);
        File folder1=new File(folderPathProperties.getPdfpath());
        if (!folder1.exists()) {
            folder1.mkdir();
        }


        File file = new File(newExcelPath);
        File file1 = new File(folderPathProperties.getPdfpath());
        File file2 = new File(folderPathProperties.getFilepath());
        String name = file.getName();
        String name1= file1.getParentFile().getName()+File.separator+file1.getName();
        String name2 = file2.getName();



        String cmd = linuxPdfProperties.getPdfprefix()+" "+linuxPdfProperties.getPdfmap()+linuxPdfProperties.getPdfmiddle()+" "+linuxPdfProperties.getPdfxls()+name2+"/"+name+" "+linuxPdfProperties.getPdfpdf()+name1+"/b.pdf";
        LinuxUtil.LinuxPdf(cmd);
        Thread.sleep(10000L);

        PaperDrive();
        LinuxPrintUtil.linuxPrint(linuxPrintProperties.getPrintprefix(),folderPathProperties.getPdfpath()+"/b.pdf",linuxPrintProperties.getPrintername(),linuxPrintProperties.getPrintsuffix2());
        Thread.sleep(10000L);

        Thread.sleep(5000L);

        stamp();
        Thread.sleep(10000L);
        LinuxPrintUtil.linuxPrint(linuxPrintProperties.getPrintprefix(),folderPathProperties.getPdfpath()+"/b.pdf",linuxPrintProperties.getPrintername(),linuxPrintProperties.getPrintsuffix2());


        Thread.sleep(10000L);
        stamp();

        PaperStop();
        Thread.sleep(5000L);

        newService.newRepeatOpenPaperDoor();
        FolderUtil.deleteFolder(folder);
        return new JsonResult<>(OK);
    }

    private void stamp(){
        Stamp.Response response = newService.newStamp(new Stamp.Request(
                2, 10, 1000, 1000,
                0, 0, 20, 1,
                30, 30));
        if (response.getData().getStatus()==-1){
            newService.newStamp(new Stamp.Request(
                    2, 10, 1000, 1000,
                    0, 0, 20, 1,
                    30, 30));
        }
    }


    @Operation(summary = "移动支付开票联系单接口")
    @PostMapping("/printYdzfkplxd")
    public JsonResult<Object> writeZlfExcel(@RequestBody Ydzfkplxd ydzfkplxd) throws Exception {

        File folder=new File(folderPathProperties.getFilepath());
        if (!folder.exists()) {
            folder.mkdir();
        }
        String ExcelPath = excelReaderWriterYdzfkplxdService.ExcelReaderWriter(ydzfkplxd,folderPathProperties.getFilepath());
        String newExcelPath = excelAddQrService.ExcelAddQr(ExcelPath, ydzfkplxd);
        File folder1=new File(folderPathProperties.getPdfpath());
        if (!folder1.exists()) {
            folder1.mkdir();
        }


        File file = new File(newExcelPath);
        File file1 = new File(folderPathProperties.getPdfpath());
        File file2 = new File(folderPathProperties.getFilepath());
        String name = file.getName();
        String name1= file1.getParentFile().getName()+File.separator+file1.getName();
        String name2 = file2.getName();

        String cmd = linuxPdfProperties.getPdfprefix()+" "+linuxPdfProperties.getPdfmap()+linuxPdfProperties.getPdfmiddle()+" "+linuxPdfProperties.getPdfxls()+name2+"/"+name+" "+linuxPdfProperties.getPdfpdf()+name1+"/c.pdf";
        log.info("生成pdf命令：{}",cmd);
        LinuxUtil.LinuxPdf(cmd);
        Thread.sleep(10000L);

        log.info("贴图获取pdf命令：{}",folderPathProperties.getPdfpath()+"c.pdf");
        String newPdfPath = pdfAddImageService.PdfAddImage(folderPathProperties.getPdfpath()+"c.pdf",bdccwsealpath,100f,250f,0.2f,0.2f);


        PaperDrive();
        LinuxPrintUtil.linuxPrint(linuxPrintProperties.getPrintprefix(),newPdfPath,linuxPrintProperties.getPrintername(),linuxPrintProperties.getPrintsuffix4());
        Thread.sleep(20000L);


        PaperStop();
        Thread.sleep(5000L);

        newService.newRepeatOpenPaperDoor();
        FolderUtil.deleteFolder(folder);
        return new JsonResult<>(OK);
    }


    @Operation(summary = "镇江市不动产交易申报单（个人增量房）接口")
    @PostMapping("/printZjsbdcjysbd")
    public JsonResult<Object> writeZlfExcel(@RequestBody Zjsbdcjysbd zjsbdcjysbd) throws Exception {

        File folder=new File(folderPathProperties.getFilepath());
        if (!folder.exists()) {
            folder.mkdir();
        }
        String ExcelPath = excelReaderWriterZjsbdcjysbdService.ExcelReaderWriter(zjsbdcjysbd, folderPathProperties.getFilepath());

        String newExcelPath = excelAddImgZjsbdcjysbdService.ExcelAddImg(ExcelPath, zjsbdcjysbd);

        File folder1=new File(folderPathProperties.getPdfpath());
        if (!folder1.exists()) {
            folder1.mkdir();
        }

        File file = new File(newExcelPath);
        File file1 = new File(folderPathProperties.getPdfpath());
        File file2 = new File(folderPathProperties.getFilepath());
        String name = file.getName();
        String name1= file1.getParentFile().getName()+File.separator+file1.getName();
        String name2 = file2.getName();

        String cmd = linuxPdfProperties.getPdfprefix()+" "+linuxPdfProperties.getPdfmap()+linuxPdfProperties.getPdfmiddle()+" "+linuxPdfProperties.getPdfxls()+name2+"/"+name+" "+linuxPdfProperties.getPdfpdf()+name1+"/printZjsbdcjysbd.pdf";

        LinuxUtil.LinuxPdf(cmd);
        Thread.sleep(10000L);

        PaperDrive();
        LinuxPrintUtil.linuxPrint(linuxPrintProperties.getPrintprefix(),folderPathProperties.getPdfpath()+"/printZjsbdcjysbd.pdf",linuxPrintProperties.getPrintername(),linuxPrintProperties.getPrintsuffix2());
        Thread.sleep(15000L);


        PaperStop();

        Thread.sleep(5000L);

        stamp();

        newService.newRepeatOpenPaperDoor();
        FolderUtil.deleteFolder(folder);

        return new JsonResult<>(OK);
    }

    @Operation(summary = "非家庭唯一及第二套住房确认书及套次打印接口")
    @PostMapping("/printFjt")
    public JsonResult<Object> writeFjtExcel(@RequestBody Fjtwyjdetzfqrs fjtwyjdetzfqrs) throws Exception {
        log.info("request:",fjtwyjdetzfqrs);

        File folder=new File(folderPathProperties.getFilepath());
        if (!folder.exists()) {
            folder.mkdir();
        }
        String ExcelPath = excelReaderWriterFjtService.ExcelReaderWriter(fjtwyjdetzfqrs, folderPathProperties.getFilepath());
        String newExcelPath = excelAddImgFjtService.ExcelAddImg(ExcelPath, fjtwyjdetzfqrs);

        File folder1=new File(folderPathProperties.getPdfpath());
        if (!folder1.exists()) {
            folder1.mkdir();
        }

        File file = new File(newExcelPath);
        File file1 = new File(folderPathProperties.getPdfpath());
        File file2 = new File(folderPathProperties.getFilepath());
        String name = file.getName();
        String name1= file1.getParentFile().getName()+File.separator+file1.getName();
        String name2 = file2.getName();

        String cmd = linuxPdfProperties.getPdfprefixa()+" "+linuxPdfProperties.getPdfmap()+linuxPdfProperties.getPdfmiddle()+" "+linuxPdfProperties.getPdfxls()+name2+"/"+name+" "+linuxPdfProperties.getPdfpdf()+name1+"/"+fjtwyjdetzfqrs.getZfqrsName()+".pdf";
        log.info("word转pdf{}",cmd);
        LinuxUtil.LinuxPdf(cmd);

        Thread.sleep(10000L);

        PaperDrive();
        LinuxPrintUtil.linuxPrint(linuxPrintProperties.getPrintprefix(),folderPathProperties.getPdfpath()+"/"+fjtwyjdetzfqrs.getZfqrsName()+".pdf",linuxPrintProperties.getPrintername(),linuxPrintProperties.getPrintsuffix1());
        Thread.sleep(20000L);
        LinuxPrintUtil.linuxPrint(linuxPrintProperties.getPrintprefix(),folderPathProperties.getPdfpath()+"/"+fjtwyjdetzfqrs.getTcName()+".pdf",linuxPrintProperties.getPrintername(),linuxPrintProperties.getPrintsuffix1());
        Thread.sleep(20000L);

        PaperStop();
        Thread.sleep(5000L);

        newService.newRepeatOpenPaperDoor();
        FolderUtil.deleteFolder(folder);
        return new JsonResult<>(OK);
    }


    @Operation(summary = "唯一及第二套承诺书及套次打印接口")
    @PostMapping("/printCns")
    public JsonResult<Object> writeCnsExcel(@RequestBody Cns cns) throws Exception {

        File folder=new File(folderPathProperties.getFilepath());
        if (!folder.exists()) {
            folder.mkdir();
        }
        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(cns);
        File jsonfile = new File(folderPathProperties.getFilepath()+"/keywords.json");
        mapper.writeValue(jsonfile, cns);
        log.info("JSON:{}",json);

        File folder1=new File(folderPathProperties.getPdfpath());
        if (!folder1.exists()) {
            folder1.mkdir();
        }
        String cmd=linuxDocxProperties.getDocxprefixdoc()+" "+fileTemplateProperties.getCns()+" "+linuxDocxProperties.getSavepath()+" "+folderPathProperties.getFilepath()+"cns.docx"+" "+linuxDocxProperties.getJsonpath()+" "+folderPathProperties.getFilepath()+"keywords.json";
        log.info("执行linux文件转译成word{}",cmd);
        //执行linux文件转译成word
        LinuxUtil.LinuxPdf(cmd);

        File file = new File(folderPathProperties.getPdfpath()+"/cns.docx");
        File file1 = new File(folderPathProperties.getPdfpath());
        File file2 = new File(folderPathProperties.getFilepath());
        String name = file.getName();
        String name1= file1.getParentFile().getName()+File.separator+file1.getName();
        String name2 = file2.getName();

        String cmd1 = linuxPdfProperties.getPdfprefixa()+" "+linuxPdfProperties.getPdfmap()+linuxPdfProperties.getPdfmiddle()+" "+linuxPdfProperties.getPdfxls()+name2+"/"+name+" "+linuxPdfProperties.getPdfpdf()+name1+"/"+cns.getZfqrsName()+".pdf";
        log.info("word转pdf{}",cmd1);
        LinuxUtil.LinuxPdf(cmd1);
        Thread.sleep(10000L);

        Base64Util.stringToImage(cns.getNsrqz(), folderPathProperties.getFilepath()+"/cnsqm1");

        log.info("pdf文件位置{}",folderPathProperties.getPdfpath()+"/"+cns.getZfqrsName()+".pdf");
        log.info("图片位置{}",folderPathProperties.getFilepath()+"cnsqm1.png");
        pdfAddImageService.PdfAddImage(folderPathProperties.getPdfpath()+"/"+cns.getZfqrsName()+".pdf",folderPathProperties.getFilepath()+"cnsqm1.png",352f,594f,0.05f,0.05f);

        log.info("pdfpath{}",folderPathProperties.getPdfpath()+"/"+cns.getZfqrsName()+".pdf");

            PaperDrive();

        LinuxPrintUtil.linuxPrint(linuxPrintProperties.getPrintprefix(),folderPathProperties.getPdfpath()+"/"+cns.getZfqrsName()+".pdf",linuxPrintProperties.getPrintername(),linuxPrintProperties.getPrintsuffix1());
        Thread.sleep(20000L);
        LinuxPrintUtil.linuxPrint(linuxPrintProperties.getPrintprefix(),folderPathProperties.getPdfpath()+"/"+cns.getTcName()+".pdf",linuxPrintProperties.getPrintername(),linuxPrintProperties.getPrintsuffix1());
        Thread.sleep(20000L);

            PaperStop();
        Thread.sleep(5000L);


        newService.newRepeatOpenPaperDoor();
        FolderUtil.deleteFolder(folder);
        return new JsonResult<>(OK);
    }


    @Operation(summary = "加名承诺书")
    @PostMapping("/printJmCns")
    public JsonResult<Object> writeJmCns(@RequestBody AddNameCns addNameCns) throws Exception {
        File folder=new File(folderPathProperties.getFilepath());
        if (!folder.exists()) {
            folder.mkdir();
        }
        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(addNameCns.getNameCns());
        File jsonfile = new File(folderPathProperties.getFilepath()+"/JmCns.json");
        mapper.writeValue(jsonfile, addNameCns.getNameCns());
        log.info("JSON:{}",json);

        File folder1=new File(folderPathProperties.getPdfpath());
        if (!folder1.exists()) {
            folder1.mkdir();
        }
        String cmd=linuxDocxProperties.getDocxprefixdoc()+" "+fileTemplateProperties.getJmcns()+" "+linuxDocxProperties.getSavepath()+" "+folderPathProperties.getFilepath()+"jmcns.docx"+" "+linuxDocxProperties.getJsonpath()+" "+folderPathProperties.getFilepath()+"JmCns.json";
        log.info("执行linux文件转译成word{}",cmd);
        //执行linux文件转译成word
        LinuxUtil.LinuxPdf(cmd);

        File file = new File(folderPathProperties.getPdfpath()+"/jmcns.docx");
        File file1 = new File(folderPathProperties.getPdfpath());
        File file2 = new File(folderPathProperties.getFilepath());
        String name = file.getName();
        String name1= file1.getParentFile().getName()+File.separator+file1.getName();
        String name2 = file2.getName();

        String cmd1 = linuxPdfProperties.getPdfprefixa()+" "+linuxPdfProperties.getPdfmap()+linuxPdfProperties.getPdfmiddle()+" "+linuxPdfProperties.getPdfxls()+name2+"/"+name+" "+linuxPdfProperties.getPdfpdf()+name1+"/h.pdf";
        log.info("word转pdf{}",cmd1);
        LinuxUtil.LinuxPdf(cmd1);
        Thread.sleep(10000L);

        Base64Util.stringToImage(addNameCns.getCnrqz(), folderPathProperties.getFilepath()+"/cnrqz1");
        log.info("图片位置{}",folderPathProperties.getFilepath()+"cnrqz1.png");
        pdfAddImageService.PdfAddImage(folderPathProperties.getPdfpath()+"h.pdf",folderPathProperties.getFilepath()+"cnrqz1.png",170f,510f,0.1f,0.1f);

        log.info("pdfpath{}",folderPathProperties.getPdfpath()+"h.pdf");
        Thread.sleep(5000L);
        PaperDrive();
        LinuxPrintUtil.linuxPrint(linuxPrintProperties.getPrintprefix(),folderPathProperties.getPdfpath()+"h.pdf",linuxPrintProperties.getPrintername(),linuxPrintProperties.getPrintsuffix1());
        Thread.sleep(20000L);
        PaperStop();
        Thread.sleep(5000L);
        newService.newRepeatOpenPaperDoor();
        FolderUtil.deleteFolder(folder);
        return new JsonResult<>(OK);
    }


    @Operation(summary = "减名约定书")
    @PostMapping("/printJmYds")
    public JsonResult<Object> writeJmYds(@RequestBody MinusNameYds minusNameYds) throws Exception {
        File folder=new File(folderPathProperties.getFilepath());
        if (!folder.exists()) {
            folder.mkdir();
        }
        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(minusNameYds.getYds());
        File jsonfile = new File(folderPathProperties.getFilepath()+"/JmYds.json");
        mapper.writeValue(jsonfile, minusNameYds.getYds());
        log.info("JSON:{}",json);

        File folder1=new File(folderPathProperties.getPdfpath());
        if (!folder1.exists()) {
            folder1.mkdir();
        }
        String cmd=linuxDocxProperties.getDocxprefixdoc()+" "+fileTemplateProperties.getJmyds()+" "+linuxDocxProperties.getSavepath()+" "+folderPathProperties.getFilepath()+"jmyds.docx"+" "+linuxDocxProperties.getJsonpath()+" "+folderPathProperties.getFilepath()+"JmYds.json";
        log.info("执行linux文件转译成word{}",cmd);
        //执行linux文件转译成word
        LinuxUtil.LinuxPdf(cmd);
        File file = new File(folderPathProperties.getPdfpath()+"/jmyds.docx");
        File file1 = new File(folderPathProperties.getPdfpath());
        File file2 = new File(folderPathProperties.getFilepath());
        String name = file.getName();
        String name1= file1.getParentFile().getName()+File.separator+file1.getName();
        String name2 = file2.getName();

        String cmd1 = linuxPdfProperties.getPdfprefixa()+" "+linuxPdfProperties.getPdfmap()+linuxPdfProperties.getPdfmiddle()+" "+linuxPdfProperties.getPdfxls()+name2+"/"+name+" "+linuxPdfProperties.getPdfpdf()+name1+"/k.pdf";
        log.info("word转pdf{}",cmd1);
        LinuxUtil.LinuxPdf(cmd1);
        Thread.sleep(10000L);
        Base64Util.stringToImage(minusNameYds.getLyrqz1(), folderPathProperties.getFilepath()+"/Lyrqz1");
        Base64Util.stringToImage(minusNameYds.getLyrqz2(), folderPathProperties.getFilepath()+"/Lyrqz2");
        pdfAddImageService.PdfAddImage(folderPathProperties.getPdfpath()+"k.pdf",folderPathProperties.getFilepath()+"Lyrqz1.png",200f,510f,0.07f,0.07f);
        pdfAddImageService.PdfAddImage(folderPathProperties.getPdfpath()+"k.pdf",folderPathProperties.getFilepath()+"Lyrqz2.png",420f,510f,0.07f,0.07f);

        log.info("pdfpath{}",folderPathProperties.getPdfpath()+"k.pdf");
        Thread.sleep(5000L);
        PaperDrive();
        LinuxPrintUtil.linuxPrint(linuxPrintProperties.getPrintprefix(),folderPathProperties.getPdfpath()+"k.pdf",linuxPrintProperties.getPrintername(),linuxPrintProperties.getPrintsuffix1());
        Thread.sleep(20000L);
        PaperStop();
        Thread.sleep(5000L);
        newService.newRepeatOpenPaperDoor();
        FolderUtil.deleteFolder(folder);
        return new JsonResult<>(OK);
    }


    public void PaperStop() {
        for (int i = 0; i < 3; i++) {
            newService.newStopPrinterPaperDrive();
        }
    }

    public void PaperDrive() {
        for (int i = 0; i < 3; i++) {
            newService.newStartPrinterPaperDrive();
        }
    }




}
